package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        List<ShoppingCart> shoppingCart =shoppingCartMapper.byId(shoppingCartDTO);
        if (shoppingCart.size()>0 &&shoppingCart!=null){
            ShoppingCart shoppingCart1 = shoppingCart.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.update(shoppingCart1);
        }else {
            ShoppingCart s = null;
            if (shoppingCartDTO.getDishId()!=null){
                Dish dish = dishMapper.byId(shoppingCartDTO.getDishId());
                s = ShoppingCart.builder().amount(dish.getPrice())
                        .dishId(shoppingCartDTO.getDishId())
                        .name(dish.getName())
                        .image(dish.getImage())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .build();
            }else {
                Setmeal setmeal = setmealMapper.setmealById(shoppingCartDTO.getSetmealId());
                s = ShoppingCart.builder()
                        .setmealId(shoppingCartDTO.getSetmealId())
                        .amount(setmeal.getPrice())
                        .name(setmeal.getName())
                        .image(setmeal.getImage())
                        .build();
            }
            s.setUserId(BaseContext.getCurrentId());
            s.setCreateTime(LocalDateTime.now());
            s.setNumber(1);
            shoppingCartMapper.save(s);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list=shoppingCartMapper.byUserId(BaseContext.getCurrentId());
        return list;
    }

    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.byUserId(BaseContext.getCurrentId());
        if (shoppingCartDTO.getDishId()!=null){
            for (ShoppingCart shoppingCart : shoppingCarts) {
                if (shoppingCart.getDishId() == shoppingCartDTO.getDishId() && shoppingCart.getDishFlavor() == shoppingCartDTO.getDishFlavor()){
                    if (shoppingCart.getNumber()==1){
                        shoppingCartMapper.deletebyDishId(shoppingCart.getDishId(),BaseContext.getCurrentId());
                    }else {
                        shoppingCart.setNumber(shoppingCart.getNumber()-1);
                       shoppingCartMapper.update(shoppingCart);
                   }
               }
           }
        }else {
            for (ShoppingCart shoppingCart : shoppingCarts) {
                if (shoppingCart.getSetmealId()==shoppingCartDTO.getSetmealId()){
                    if (shoppingCart.getNumber()==1){
                        shoppingCartMapper.deletebySetmealId(shoppingCart.getSetmealId(),BaseContext.getCurrentId());
                    }else {
                        shoppingCart.setNumber(shoppingCart.getNumber()-1);
                        shoppingCartMapper.update(shoppingCart);
                    }
                }
            }
        }
    }
}
